{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "from __future__ import print_function, absolute_import\n",
    "from gm.api import *\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 可以直接提取数据，掘金终端需要打开，接口取数是通过网络请求的方式，效率一般，行情数据可通过subscribe订阅方式\n",
    "# 设置token， 查看已有token ID,在用户-密钥管理里获取\n",
    "set_token('bf739ec28ad507f391a3c3e99ed0365007da88f8')\n",
    "\n",
    "# set_token('ca9c4532786a122aa5ceb3fd726432862e046f91')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# get_instrumentinfos - 查询交易标的基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get_instrumentinfos(symbols=None, exchanges=None, sec_types=None, names=None, fields=None, df=False)\n",
    "df_code = get_instrumentinfos(symbols=None, \n",
    "                                exchanges=None, \n",
    "                                sec_types=SEC_TYPE_STOCK, \n",
    "                                fields='symbol, listed_date, delisted_date', \n",
    "                                df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>sec_type</th>\n",
       "      <th>exchange</th>\n",
       "      <th>sec_id</th>\n",
       "      <th>sec_name</th>\n",
       "      <th>sec_abbr</th>\n",
       "      <th>price_tick</th>\n",
       "      <th>listed_date</th>\n",
       "      <th>delisted_date</th>\n",
       "      <th>trade_n</th>\n",
       "      <th>...</th>\n",
       "      <th>option_margin_ratio1</th>\n",
       "      <th>option_margin_ratio2</th>\n",
       "      <th>sec_type_ext</th>\n",
       "      <th>option_type</th>\n",
       "      <th>call_or_put</th>\n",
       "      <th>underlying_symbol</th>\n",
       "      <th>is_strike_price_adjusted</th>\n",
       "      <th>exercise_price</th>\n",
       "      <th>conversion_price</th>\n",
       "      <th>conversion_start_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.688192</td>\n",
       "      <td>1</td>\n",
       "      <td>SHSE</td>\n",
       "      <td>688192</td>\n",
       "      <td>迪哲医药</td>\n",
       "      <td>DZYY</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2021-12-10 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.601868</td>\n",
       "      <td>1</td>\n",
       "      <td>SHSE</td>\n",
       "      <td>601868</td>\n",
       "      <td>中国能建</td>\n",
       "      <td>ZGNJ</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2021-09-28 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.688388</td>\n",
       "      <td>1</td>\n",
       "      <td>SHSE</td>\n",
       "      <td>688388</td>\n",
       "      <td>嘉元科技</td>\n",
       "      <td>JYKJ</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2019-07-22 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SHSE.600036</td>\n",
       "      <td>1</td>\n",
       "      <td>SHSE</td>\n",
       "      <td>600036</td>\n",
       "      <td>招商银行</td>\n",
       "      <td>ZSYH</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2002-04-09 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHSE.605319</td>\n",
       "      <td>1</td>\n",
       "      <td>SHSE</td>\n",
       "      <td>605319</td>\n",
       "      <td>无锡振华</td>\n",
       "      <td>WXZH</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2021-06-07 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5481</th>\n",
       "      <td>SZSE.002115</td>\n",
       "      <td>1</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>002115</td>\n",
       "      <td>三维通信</td>\n",
       "      <td>SWTX</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2007-02-15 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5482</th>\n",
       "      <td>SZSE.002207</td>\n",
       "      <td>1</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>002207</td>\n",
       "      <td>准油股份</td>\n",
       "      <td>ZYGF</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2008-01-28 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5483</th>\n",
       "      <td>SZSE.002899</td>\n",
       "      <td>1</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>002899</td>\n",
       "      <td>英派斯</td>\n",
       "      <td>YPS</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2017-09-15 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5484</th>\n",
       "      <td>SZSE.300449</td>\n",
       "      <td>1</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>300449</td>\n",
       "      <td>汉邦高科</td>\n",
       "      <td>HBGK</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2015-04-22 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5485</th>\n",
       "      <td>SZSE.300791</td>\n",
       "      <td>1</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>300791</td>\n",
       "      <td>仙乐健康</td>\n",
       "      <td>XLJK</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2019-09-25 00:00:00+08:00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5486 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           symbol  sec_type exchange  sec_id sec_name sec_abbr  price_tick  \\\n",
       "0     SHSE.688192         1     SHSE  688192     迪哲医药     DZYY        0.01   \n",
       "1     SHSE.601868         1     SHSE  601868     中国能建     ZGNJ        0.01   \n",
       "2     SHSE.688388         1     SHSE  688388     嘉元科技     JYKJ        0.01   \n",
       "3     SHSE.600036         1     SHSE  600036     招商银行     ZSYH        0.01   \n",
       "4     SHSE.605319         1     SHSE  605319     无锡振华     WXZH        0.01   \n",
       "...           ...       ...      ...     ...      ...      ...         ...   \n",
       "5481  SZSE.002115         1     SZSE  002115     三维通信     SWTX        0.01   \n",
       "5482  SZSE.002207         1     SZSE  002207     准油股份     ZYGF        0.01   \n",
       "5483  SZSE.002899         1     SZSE  002899      英派斯      YPS        0.01   \n",
       "5484  SZSE.300449         1     SZSE  300449     汉邦高科     HBGK        0.01   \n",
       "5485  SZSE.300791         1     SZSE  300791     仙乐健康     XLJK        0.01   \n",
       "\n",
       "                   listed_date             delisted_date  trade_n  ...  \\\n",
       "0    2021-12-10 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "1    2021-09-28 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "2    2019-07-22 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "3    2002-04-09 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "4    2021-06-07 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "...                        ...                       ...      ...  ...   \n",
       "5481 2007-02-15 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "5482 2008-01-28 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "5483 2017-09-15 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "5484 2015-04-22 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "5485 2019-09-25 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
       "\n",
       "      option_margin_ratio1  option_margin_ratio2  sec_type_ext option_type  \\\n",
       "0                      0.0                   0.0             0               \n",
       "1                      0.0                   0.0             0               \n",
       "2                      0.0                   0.0             0               \n",
       "3                      0.0                   0.0             0               \n",
       "4                      0.0                   0.0             0               \n",
       "...                    ...                   ...           ...         ...   \n",
       "5481                   0.0                   0.0             0               \n",
       "5482                   0.0                   0.0             0               \n",
       "5483                   0.0                   0.0             0               \n",
       "5484                   0.0                   0.0             0               \n",
       "5485                   0.0                   0.0             0               \n",
       "\n",
       "      call_or_put  underlying_symbol  is_strike_price_adjusted exercise_price  \\\n",
       "0                                                        False            0.0   \n",
       "1                                                        False            0.0   \n",
       "2                                                        False            0.0   \n",
       "3                                                        False            0.0   \n",
       "4                                                        False            0.0   \n",
       "...           ...                ...                       ...            ...   \n",
       "5481                                                     False            0.0   \n",
       "5482                                                     False            0.0   \n",
       "5483                                                     False            0.0   \n",
       "5484                                                     False            0.0   \n",
       "5485                                                     False            0.0   \n",
       "\n",
       "     conversion_price conversion_start_date  \n",
       "0                 0.0                  None  \n",
       "1                 0.0                  None  \n",
       "2                 0.0                  None  \n",
       "3                 0.0                  None  \n",
       "4                 0.0                  None  \n",
       "...               ...                   ...  \n",
       "5481              0.0                  None  \n",
       "5482              0.0                  None  \n",
       "5483              0.0                  None  \n",
       "5484              0.0                  None  \n",
       "5485              0.0                  None  \n",
       "\n",
       "[5486 rows x 24 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "get_symbol_infos - 查询标的基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get_symbol_infos(sec_type1, sec_type2=None, exchanges=None, symbols=None, df=False)\n",
    "df=get_symbol_infos(sec_type1=1010, #1010: 股票， 1020: 基金， 1030: 债券 ， 1040: 期货， 1050: 期权， 1060: 指数，1070：板块.\n",
    "                    sec_type2=None, \n",
    "                    exchanges=None, \n",
    "                    symbols=None, \n",
    "                    df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>listed_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>1999-11-10 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.600001</td>\n",
       "      <td>1998-01-22 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.600002</td>\n",
       "      <td>1998-04-08 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SHSE.600003</td>\n",
       "      <td>1999-08-10 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHSE.600004</td>\n",
       "      <td>2003-04-28 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5481</th>\n",
       "      <td>SZSE.301588</td>\n",
       "      <td>2024-03-13 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5482</th>\n",
       "      <td>SZSE.301589</td>\n",
       "      <td>2024-02-08 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5483</th>\n",
       "      <td>SZSE.301591</td>\n",
       "      <td>2024-02-28 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5484</th>\n",
       "      <td>SZSE.301596</td>\n",
       "      <td>2024-05-13 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5485</th>\n",
       "      <td>SZSE.301603</td>\n",
       "      <td>2024-07-10 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5486 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           symbol               listed_date\n",
       "0     SHSE.600000 1999-11-10 00:00:00+08:00\n",
       "1     SHSE.600001 1998-01-22 00:00:00+08:00\n",
       "2     SHSE.600002 1998-04-08 00:00:00+08:00\n",
       "3     SHSE.600003 1999-08-10 00:00:00+08:00\n",
       "4     SHSE.600004 2003-04-28 00:00:00+08:00\n",
       "...           ...                       ...\n",
       "5481  SZSE.301588 2024-03-13 00:00:00+08:00\n",
       "5482  SZSE.301589 2024-02-08 00:00:00+08:00\n",
       "5483  SZSE.301591 2024-02-28 00:00:00+08:00\n",
       "5484  SZSE.301596 2024-05-13 00:00:00+08:00\n",
       "5485  SZSE.301603 2024-07-10 00:00:00+08:00\n",
       "\n",
       "[5486 rows x 2 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['symbol', 'listed_date']]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
